//index.js
//获取应用实例
const app = getApp()
let Axios = require('../../../common/js/ajax.js'); //引入Axios方法
let Mtils = require('../../../common/js/Mtils.min.js');
Mtils = Mtils.Mtils
var page = 1;
var switch_p = true
Page({
	data: {
		openid: '',
		nickName: '',
		avatarUrl: '',
		curentNav: 2, //当前导航
		address: [],
		curSelect: 0,
		goods_id: 0,
		address_id: 0,
		isIpx:app.globalData.isIpx?true:false
	},
	//事件处理函数
	onLoad: function(e) {
		var _this = this;
		this.setData({
			goods_id: e.id,
			address_id: e.address_id
		})
		this.uid = app.globalData.uid || ''
		this.Md5 = Mtils.security.hex_md5(this.uid) + app.globalData.secret;
		this.Md5 = Mtils.security.hex_md5(this.Md5);
		this.timesp = Mtils.now();

	},
	onShow(e) {
		var _this = this;
		this.load = this.selectComponent("#loading");
		Axios.get(app.globalData.baseUrl + 'App/User/UserInfo/my_address_list', {
			uid: this.uid,
			time: this.timesp,
			sign: this.Md5
		}, (res) => {
			console.log(res)
			if (res.status == 1) {
				var data = res.info
				_this.setData({
					address: data
				})
				_this.data.address.forEach(function(item, key) {
					if (item.id == _this.data.address_id) {
						_this.setData({
							curSelect: key
						})
					}
				})
				_this.load.loadIng()
			} else {
				this.showModal(res)
			}
		})
	},
	onReady: function() {
		wx.setNavigationBarTitle({
			title: '我的地址'
		})
	},
	onReachBottom: function() {
		var _this = this;
		wx.showLoading({
			title: '玩命加载中',
		})
		if (switch_p) {
			switch_p = false
			page = page + 1;
			Axios.get(app.globalData.baseUrl + 'App/User/UserInfo/my_address_list?p=' + page, {
				uid: this.uid,
				time: this.timesp,
				sign: this.Md5,
			}, (res) => {
				console.log(res)
				if (res.status == 1) {
					var data = res.info
					var temp = _this.data.address.concat(data)
					_this.setData({
						address: temp
					})
					_this.data.address.forEach(function(item, key) {
						if (item.id == _this.data.address_id) {
							_this.setData({
								curSelect: key
							})
						}
					})
					switch_p = true
					wx.hideLoading();
				} else {
					this.showModal(res)
				}
			})
		}
	},
	bindRegionChange: function(e) {
		console.log('picker发送选择改变，携带值为', e.detail.value)
		this.setData({
			region: e.detail.value
		})
	},
	handleSelectTap: function(e) { //选择地址
		var _this = this;
		var index = e.currentTarget.dataset.index
		this.setData({
			curSelect: index
		})
	},
	subAddress: function() {
		var index = this.data.curSelect
		var id = this.data.address[index].id;
		var full_name = this.data.address[index].full_name;
		var phone = this.data.address[index].mobile;
		var address = this.data.address[index].province + ' ' + this.data.address[index].city + ' ' + this.data.address[
			index].area + ' ' + this.data.address[index].address;
		var goods_id = this.data.goods_id;
		wx.navigateTo({
			url: '/pages/order/order?id=' + id + '&full_name=' + full_name + '&mobile=' + phone + '&address=' + address +
				'&goods_id=' + goods_id
		})
	}

})
